package com.tepth.maintenancesystem.view;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.nshmura.recyclertablayout.RecyclerTabLayout;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.bean.AlarmObj;
import com.tepth.maintenancesystem.bean.CompletedObj;
import com.tepth.maintenancesystem.adapter.CompletedAdapter;
import com.tepth.maintenancesystem.adapter.InitViewPager;
import com.tepth.maintenancesystem.adapter.MyAlarmAdapter;
import com.tepth.maintenancesystem.adapter.VehicleDetailsAdapter;
import com.tepth.maintenancesystem.untils.DimensionConvert;

import java.util.ArrayList;
import java.util.List;

public class VehicleDetailsActivity extends AppCompatActivity implements InitViewPager {

    private RecyclerTabLayout mRecyclerTabLayout;
    private List<AlarmObj> alarmList;
    private SuperRecyclerView alarm_list;
    private MyAlarmAdapter adapter;

    private List<CompletedObj> completedList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vehicle_details);
        alarmList=new ArrayList<>();
        VehicleDetailsAdapter adapter = new VehicleDetailsAdapter(this);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(adapter);
        mRecyclerTabLayout = (RecyclerTabLayout) findViewById(R.id.recycler_tab_layout);
        mRecyclerTabLayout.setTabMinWidth(DimensionConvert.getWidth3(this));//设置tab宽度
        mRecyclerTabLayout.setUpWithViewPager(viewPager);

    }

    @Override
    public void initViewPager(View view, int position) {
        switch (position) {
            case 0:
                newAlarm();
                alarm_list = (SuperRecyclerView) view.findViewById(R.id.lv_untreated);
                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                alarm_list.setLayoutManager(layoutManager);
                alarm_list.setRefreshEnabled(true);//可以定制是否开启下拉刷新
                alarm_list.setLoadMoreEnabled(true);//可以定制是否开启加载更多
                alarm_list.setLoadingListener(new PullToRefreshListener());//下拉刷新，上拉加载的监听
                alarm_list.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//下拉刷新的样式
                alarm_list.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//上拉加载的样式
                alarm_list.setArrowImageView(R.drawable.arrow_down);//设置下拉箭头

                adapter = new MyAlarmAdapter(this,alarmList);
                alarm_list.setAdapter(adapter);
                break;
            case 1:
                addAlarmCompleted();
                CompletedAdapter completedAdapter=new CompletedAdapter(this,completedList);
                RecyclerView list=(RecyclerView) view.findViewById(R.id.lv_completed);
                LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
                layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                list.setLayoutManager(layoutManager2);
                list.setAdapter(completedAdapter);
                break;
            case 2:
                break;
            default:
                break;
        }

    }

    private void addAlarm(){
        alarmList.add(new AlarmObj("发动机报警","5","2016/11/28","1","1"));
        alarmList.add(new AlarmObj("发动机报警","2","2016/11/28","2","1"));
        alarmList.add(new AlarmObj("发动机报警","3","2016/11/28","2","1"));
        alarmList.add(new AlarmObj("发动机报警","1","2016/11/28","3","0"));
        alarmList.add(new AlarmObj("发动机报警","3","2016/11/28","3","0"));
    }

    private void newAlarm(){
        alarmList.clear();
        addAlarm();
    }

    private void addAlarmCompleted(){
        completedList=new ArrayList<>();

        completedList.add(new CompletedObj(0,"2016/11/28","",""));
        //Add Level 2 Item
        completedList.add(new CompletedObj(1,"发动机","换发动机配件","10:10"));
        completedList.add(new CompletedObj(1,"轮胎","换轮胎","13:15"));
        completedList.add(new CompletedObj(1,"底盘","检查刹车","15：10"));

        completedList.add(new CompletedObj(0,"2016/11/14","",""));
        //Add Level 2 Item
        completedList.add(new CompletedObj(1,"轮胎","换轮胎","13:15"));
        completedList.add(new CompletedObj(1,"底盘","检查刹车","15：10"));
        completedList.add(new CompletedObj(1,"发动机","换发动机配件","10:10"));

        completedList.add(new CompletedObj(0,"2016/11/02","",""));
        //Add Level 2 Item
        completedList.add(new CompletedObj(1,"轮胎","换轮胎","13:15"));
        completedList.add(new CompletedObj(1,"底盘","检查刹车","15：10"));
        completedList.add(new CompletedObj(1,"发动机","换发动机配件","10:10"));

        completedList.add(new CompletedObj(0,"2016/10/23","",""));
        //Add Level 2 Item
        completedList.add(new CompletedObj(1,"轮胎","换轮胎","13:15"));
        completedList.add(new CompletedObj(1,"底盘","检查刹车","15：10"));
    }

    private class PullToRefreshListener implements SuperRecyclerView.LoadingListener{

        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newAlarm();
                    alarm_list.completeRefresh();
                    adapter.notifyDataSetChanged();
                }
            }, 3000);
        }

        @Override
        public void onLoadMore() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addAlarm();
                    alarm_list.completeLoadMore();
                }
            }, 3000);
        }
    }
}
